package org.shangrila.bme.mb.mobile;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.primefaces.event.FileUploadEvent;
import org.shangrila.app.sys.cfg.AppInitInfo;
import org.shangrila.app.sys.shiro.SecUtil;
import org.shangrila.app.wfm.mb.FileUploadProcessor;
import org.shangrila.bme.eo.AppFileInfo;
import org.shangrila.bme.eo.BmeContProd;
import org.shangrila.bme.eo.BmeContract;
import org.shangrila.bme.eo.BmeMaterial;
import org.shangrila.bme.eo.BmeRptInfo;
import org.shangrila.bme.mb.BmeCommEntityService;
import org.shangrila.comm.dao.DbAccess;
import org.shangrila.comm.helper.IDHelper;
import org.shangrila.comm.utils.FileUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Named
@SessionScoped
public class MobileRptInfoMB implements Serializable {
	private static final long serialVersionUID = -512146464217112L;
	private final Logger log = LoggerFactory.getLogger(this.getClass());

	private List<BmeContract> bmeContracts;
	private BmeContract selectedContract = new BmeContract(); 
	
	private List<BmeContProd> bmeContProds; 	
	private BmeContProd selectedContProd = new BmeContProd(); 
	
	private BmeRptInfo bmeRptInfo;
	private BmeMaterial bmeMaterial = new BmeMaterial();
	
	private String operator;
	
	private String uploadedFile="";
	private String uploadedFileImage="logo_head.jpg";
	
	private String uploadForType="9";
	private String uploadForId="0";
	private String uploadForName="0";

	private String entryPage="mb_rptinfo_input.xhtml";//报工单录入
	
	@Inject
	private DbAccess db;
	@Inject
	BmeCommEntityService bmeCommEntityService;

	@PostConstruct
    public void init() {
		try{
			operator = SecUtil.getCachedShiroUser().getUserno();
		}
		catch(Exception ex){
			operator="bme";
		}
		queryContracts();
    }
	
	public void queryContracts() {
    	bmeContracts = bmeCommEntityService.getBmeContracts();
	}
	
	public String queryContProd() {
		bmeContProds = selectedContract.getBmeContProds();
		bmeCommEntityService.setBmeContProd(bmeContProds);
		if(bmeContProds!=null && bmeContProds.size()>0) {
			selectedContProd = bmeContProds.get(0);
			bmeMaterial = selectedContProd.getBmeMaterial();
			bmeRptInfo = new BmeRptInfo();
			bmeRptInfo.setId(IDHelper.genStrIDs(16));			
			return entryPage;
		}else {
			return null;
		}
		
	}
	
	public void onSelectBmeMaterial() {
		bmeMaterial = selectedContProd.getBmeMaterial();
	}
	
	public void saveRptInfo() {
		db.beginTransaction();
		
		bmeRptInfo.setBmeMaterial(bmeMaterial);
		bmeRptInfo.setCreateTime(new Date());
		bmeRptInfo.setContName(selectedContract.getName());
		bmeRptInfo.setBmeContract(selectedContract);
		bmeRptInfo.setStartDate(new Date());
		bmeRptInfo.setStatus("1");
		bmeRptInfo.setOperator(operator);
		
		db.insert(bmeRptInfo);
		db.commitTransaction();
		
		selectedContProd = bmeContProds.get(0);
		bmeMaterial = selectedContProd.getBmeMaterial();
		
		bmeRptInfo = new BmeRptInfo();
		bmeRptInfo.setId(IDHelper.genStrIDs(16));
		AppInitInfo.showMessage("报工单录入", "处理成功");
		
		uploadForId=bmeRptInfo.getId();
		uploadForName=bmeRptInfo.getContName();

	}
	
    // handle File Upload
	public void handleFileUpload(FileUploadEvent event) {
		log.info("handleFileUpload...");
		
		String fileId = IDHelper.genStrID(24);
		uploadedFile = event.getFile().getFileName();//上传文件路径名
		
		String fileName = uploadedFile.substring(uploadedFile.lastIndexOf(AppInitInfo.WOS_SEPERATOR) + 1);

		String fileExt = FileUtil.getFileExtension(new File(fileName));//上传文件路径名扩展名
		String webfileName = AppInitInfo.BME_UPLOAD_FILE_PATH + fileId +"." + fileExt;
		log.info("webfileName: "+ webfileName);
		
		try {
			FileUploadProcessor.saveInputStreamAsFile(event.getFile().getInputstream(),webfileName);
			//文件存入数据库AppFileInfo
			AppFileInfo afi = new AppFileInfo();
			afi.setId(fileId);
			afi.setForType(uploadForType);
			afi.setForId(uploadForId);
			afi.setForName(uploadForName);
			afi.setOperator(operator);
			afi.setUpTime(new Date());
			afi.setUpdName(uploadedFile);
			afi.setFilePath(webfileName);
			afi.setFileExt(fileExt);
			//afi.setRemarks(remarks);
			db.begin();
			db.insert(afi);
			db.commit();
			
			uploadedFileImage = fileId+"."+fileExt;		
			log.info(uploadedFile + " is uploaded.");

			AppInitInfo.showMessage("文件上传", "处理成功");

		} catch (IOException e) {
			AppInitInfo.showMessage("文件上传", "处理失败");
			log.error(uploadedFile + " is uploaded failly.");
		} 		
	}

	public String nullAction() {
		return null;
	}
	
	public List<BmeContract> getBmeContracts() {
		return bmeContracts;
	}

	public void setBmeContracts(List<BmeContract> bmeContracts) {
		this.bmeContracts = bmeContracts;
	}
	
	public BmeContract getSelectedContract() {
		return selectedContract;
	}

	public void setSelectedContract(BmeContract selectedContract) {
		this.selectedContract = selectedContract;
	}

	public List<BmeContProd> getBmeContProds() {
		return bmeContProds;
	}

	public void setBmeContProds(List<BmeContProd> bmeContProds) {
		this.bmeContProds = bmeContProds;
	}

	public BmeContProd getSelectedContProd() {
		return selectedContProd;
	}

	public void setSelectedContProd(BmeContProd selectedContProd) {
		this.selectedContProd = selectedContProd;
	}

	public BmeRptInfo getBmeRptInfo() {
		return bmeRptInfo;
	}

	public void setBmeRptInfo(BmeRptInfo bmeRptInfo) {
		this.bmeRptInfo = bmeRptInfo;
	}

	public BmeMaterial getBmeMaterial() {
		return bmeMaterial;
	}

	public void setBmeMaterial(BmeMaterial bmeMaterial) {
		this.bmeMaterial = bmeMaterial;
	}

	public String getUploadedFile() {
		return uploadedFile;
	}

	public void setUploadedFile(String uploadedFile) {
		this.uploadedFile = uploadedFile;
	}

	public String getUploadedFileImage() {
		return uploadedFileImage;
	}

	public void setUploadedFileImage(String uploadedFileImage) {
		this.uploadedFileImage = uploadedFileImage;
	}

	public String getUploadForType() {
		return uploadForType;
	}

	public void setUploadForType(String uploadForType) {
		this.uploadForType = uploadForType;
	}

	public String getUploadForId() {
		return uploadForId;
	}

	public void setUploadForId(String uploadForId) {
		this.uploadForId = uploadForId;
	}

	public String getUploadForName() {
		return uploadForName;
	}

	public void setUploadForName(String uploadForName) {
		this.uploadForName = uploadForName;
	}
	
}